package pl.wroc.uni.ii.puzzle.game;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.Rect;

public class ImageFragment {

	private final static Matrix horizontalTransformationMatrix = new Matrix();
	private final static Matrix verticalTransformationMatrix = new Matrix();
	static {
		horizontalTransformationMatrix.preScale(-1, 1); //filp on X axis
		verticalTransformationMatrix.preScale(1, -1);
	}
	private Bitmap cellImage;
	private Rect currentPositionRect;
	
	public ImageFragment(Bitmap cellImage, Rect startPositionRect) {
		currentPositionRect = new Rect(startPositionRect);
		this.cellImage = cellImage;
	}
	
	public Rect getCurrentPositionRect() {
		return currentPositionRect;
	}
	
	public Bitmap getCellImage() {
		return cellImage;
	}
	
	public boolean isInProperPosition(Rect startPositionRect) {
		return (startPositionRect.left == currentPositionRect.left
				&& startPositionRect.top == currentPositionRect.top);
	}
	public void reflectHorizontal() {
		int left = currentPositionRect.left;
		currentPositionRect.left = currentPositionRect.right;
		currentPositionRect.right = left;
		reflectHorizontalCellImage();
	}

	private void reflectHorizontalCellImage() {
        Bitmap image = Bitmap.createBitmap(cellImage, 0, 0, cellImage.getWidth(), cellImage.getHeight(), horizontalTransformationMatrix, false);
        cellImage = image;
	}

	public void reflectVertical() {
		int top = currentPositionRect.top;
		currentPositionRect.top = currentPositionRect.bottom;
		currentPositionRect.bottom = top;
		reflectVerticalCellImage();
	}

	private void reflectVerticalCellImage() {
		 Bitmap image = Bitmap.createBitmap(cellImage, 0, 0, cellImage.getWidth(), cellImage.getHeight(), verticalTransformationMatrix, false);
	     cellImage = image;
	}
}
